{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SubFunction Was Imported\n",
      "Type: <class 'list'>, ['字符串', 12, 3.45, ['6']]\n"
     ]
    }
   ],
   "source": [
    "# 列表的类型\n",
    "from SubFunc import printType\n",
    "\n",
    "c = [\"字符串\", 12, 3.45, [\"6\"]]  # 列表可以包含多种类型\n",
    "\n",
    "printType(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表增加元素\n",
    "a: list = [\"1\"]\n",
    "print(a)\n",
    "a.append(\"2\")  # 追加元素\n",
    "print(a)\n",
    "a.insert(1, \"哈哈\")  # 在第x个位置插入文本,并将x后面的内容往后移\n",
    "print(a)\n",
    "b = [6, 7]\n",
    "a.extend(b)  # 追加列表\n",
    "print(a)\n",
    "c = a + [8, 9]  # 合并两个列表并返回值,不会修改a\n",
    "print(a)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表删除元素\n",
    "\n",
    "# 直接pop\n",
    "a = [1, 2, 3, 4, 5]\n",
    "print(a)\n",
    "b = a.pop()  # 注意pop后面要带括号\n",
    "print(a)  # 少了最后一个元素\n",
    "print(b)  # 从a获得最后一个元素\n",
    "\n",
    "# pop并指定下标\n",
    "a = [1, 2, 3, 4, 5]\n",
    "print(a)\n",
    "b = a.pop(2 - 1)  # 注意pop后面要带括号\n",
    "print(a)  # 少了第2个元素\n",
    "print(b)  # 从a获得第2个元素\n",
    "\n",
    "\n",
    "# remove\n",
    "a = [1, 2, 3, 1, 2, 3]\n",
    "print(a)\n",
    "a.remove(2)  # 删除从左往右第一个对应的元素,不会删除所有重复的元素\n",
    "print(a)\n",
    "try:\n",
    "    a.remove(4)  # 删除不存在的元素时会报错\n",
    "    print(a)\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "\n",
    "# del\n",
    "a = [1, 2, 3, 1, 2, 3]\n",
    "del a  # del之后,a不存在.并且编辑器也会提示a变量不存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表修改切片查询\n",
    "\n",
    "# 直接修改\n",
    "a = [1, 2, 3, 4, 5]\n",
    "a[1 - 1] = 6  # 直接修改第一个元素\n",
    "print(a)\n",
    "\n",
    "# 直接修改超出下标的元素会报错\n",
    "try:\n",
    "    a[10 - 1]\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "\n",
    "# 切片\n",
    "b = a[2 - 1 : 4]  # 获取第2至4的元素,并返回\n",
    "print(b)\n",
    "c = a[6 - 1 : 7]  # 获取不存在的第6至7的元素,不会报错\n",
    "print(c)\n",
    "\n",
    "# count查询数量\n",
    "a = [1, 2, 2, 3, 3, 3]\n",
    "d = a.count(3)\n",
    "print(d)\n",
    "d = a.count(4)  # 查不到就会返回0\n",
    "print(d)\n",
    "\n",
    "# 求最大值最小值\n",
    "a = [1, 2, 3, 4, 5, 6]\n",
    "print(max(a))\n",
    "print(min(a))\n",
    "b = [\"a\", \"b\", \"c\"]\n",
    "print(max(b))  # 文本也可以比大小\n",
    "print(min(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表推导式\n",
    "a = [i for i in range(2, 10 + 1, 1)]  # 创造一个包含从2到10,步长为1的列表\n",
    "print(a)\n",
    "\n",
    "\n",
    "# 封装列表推导式\n",
    "def lieBiaoTuiDaoShi(startNum, endNum, step=1, inLambda=(lambda x: True)) -> list:\n",
    "    return [i for i in range(startNum, endNum + 1, step) if inLambda(i)]\n",
    "\n",
    "\n",
    "b = lieBiaoTuiDaoShi(1, 20)\n",
    "print(b)\n",
    "\n",
    "# 列表推导式加条件\n",
    "c = lieBiaoTuiDaoShi(3, 20, 1, lambda x: x % 2 == 0)  # 筛选出余2为0的情况\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元组\n",
    "\n",
    "a = tuple()\n",
    "print(a)\n",
    "\n",
    "a = tuple()\n",
    "a = (1,)  # 只有一个元素的元组,需要添加逗号用于区分\n",
    "b = a[1 - 1]\n",
    "print(b)\n",
    "\n",
    "a = tuple()\n",
    "a = (1, 2, 3)\n",
    "try:\n",
    "    a[1 - 1] = 4  # type: ignore # 修改元组时会报错\n",
    "except Exception as e:\n",
    "    print(e)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
